const {floor} = require("lodash");
const {round} = require("lodash/math");

function wuyuan(R0, x, w = [0.25, 0.25, 0.25, 0.25]) {
    let n = R0.length;//4

    let mm = R0[0].length;//8

    let m = mm / 2;//4
    let a = [], b = [];
    for (let i = 0; i < n; i++) {
        a[i] = []
        b[i] = [];
        for (let j = 0; j < m; j++) {
            a[i][j] = R0[i][j * 2];
            b[i][j] = R0[i][j * 2 + 1];
        }
    }
    let ap = [], bp = [];
    for (let i = 0; i < n; i++) {
        ap[i] = R0[i][0];
        bp[i] = R0[i][mm - 1];
    }
    let fah = x.length;//1
    let fal = x[0].length;//4
    let k = [];
    for (let i = 0; i < n; i++) {
        k[i] = [];
        for (let j = 0; j < m; j++) {
            k[i][j] = 0;
        }
    }
    let index = [];
    let jstar = 0;
    for (let g = 0; g < fal; g++) {
        for (let i = 0; i < fah; i++) {
            for (let j = 0; j < m; j++) {
                if (a[i][j] === b[i][j]) {
                    if (x[i][g] === a[i][j]) {
                        k[i][j] = 0;
                    } else {
                        let pp = Math.abs(x[i][g] - 0.5 * (ap[i] + bp[i])) - 0.5 * (bp[i] - ap[i]);
                        k[i][j] = Math.abs(x[i][g] - a[i][j]) / (pp - Math.abs(x[i][g] - a[i][j]));
                    }
                } else {

                    if (x[i][g] >= a[i][j] && x[i][g] <= b[i][j]) {
                        let p = Math.abs(x[i][g] - 0.5 * (a[i][j] + b[i][j])) - 0.5 * (b[i][j] - a[i][j]);
                        k[i][j] = -p / Math.abs(b[i][j] - a[i][j]);
                    } else {
                        let p = Math.abs(x[i][g] - 0.5 * (a[i][j] + b[i][j])) - 0.5 * (b[i][j] - a[i][j]);
                        let pp = Math.abs(x[i][g] - 0.5 * (ap[i] + bp[i])) - 0.5 * (bp[i] - ap[i]);
                        k[i][j] = p / (pp - p);
                    }
                }
            }
        }
        let kp = [];
        for (let j = 0; j < m; j++) {
            let sum = 0;
            for (let i = 0; i < w.length; i++) {
                sum += w[i] * k[i][j];
            }
            kp[j] = sum;
        }
        let kpmaxIndex = 0;
        let kpmax = kp[0];
        let kpmin = kp[0];
        for (let i = 1; i < kp.length; i++) {
            if (kp[i] > kpmax) {
                kpmax = kp[i];
                kpmaxIndex = i;
            }
            if (kp[i] < kpmin) {
                kpmin = kp[i];
            }
        }
        index.push(kpmaxIndex);

        let avkpp = [];
        let avkppSum = 0;
        for (let i = 0; i < m; i++) {
            avkpp[i] = (kp[i] - kpmin) / (kpmax - kpmin);

            avkppSum += avkpp[i];
        }

        let jup = 0;
        for (let i = 0; i < m; i++) {
            jup += i * avkpp[i];
        }
        jstar = jup / avkppSum;

    }
    console.log(jstar)
    jstar = Math.round(jstar)
    // 根据评价结果，判断属于哪一级别
    if (jstar == 0) {
        return "差";
    } else if (jstar == 1) {
        return "中";
    } else if (jstar == 2) {
        return "良";
    } else if (jstar == 3) {
        return "优";
    } else {
        return '未知'
    }
}

function jingdainyu(fullMark) { //经典域  参数为问题满分分数
    let arr = []//     一个问题的经典域
    //差等级低于满分的60%,区间为0-60%
    arr.push(0)
    console.log(typeof fullMark)
    arr.push(fullMark * 0.5)
    //中等级为满分的60%-75%,采取向下取整.即如果上一等级右区间为小数则取整数部分为最终右区间,例:如果差等级的右区间即(fullmark*0.6为小数,25.5),则
    arr.push(arr[1] + 1)                                                         //差区间的结果为(0,25],则中区间为(26-fullmark*0.75]
    arr.push(fullMark * 0.7)
    //良区间 75%-85%
    arr.push(arr[3] + 1)
    arr.push(fullMark * 0.9)
    //优区间 85%-100%
    arr.push(arr[5] + 1)
    arr.push(fullMark)
    return arr


}


//
const R0 =
    [
        [0, 10, 11, 20, 21, 30, 31, 40], //知标签,优良中查区间
        [0, 10, 11, 20, 21, 30, 31, 40], //情标签,优良中查区间
        [0, 10, 11, 20, 21, 30, 31, 40], //意标签,优良中查区间
        [0, 10, 11, 20, 21, 30, 31, 40], //行标签,优良中查区间
    ];
const qq =
    [
        [0, 15, 16, 21, 22, 27, 28, 30],
        [0, 20, 21, 28, 29, 36, 37, 40],
        [0, 25, 26, 35, 36, 45, 46, 50],
        [0.30, 31, 42, 43, 54, 55, 60]
    ]


const x = [
    // [35, 33, 33, 34], //知21分,情27分,意33分,行24分,这是一份问卷的结果
    // [32, 32, 32, 33],
    [11, 12, 20, 30],
    // [15, 15, 29, 39],
    // [40, 40, 40, 40],
    // [40, 40, 40, 40],
    // [40, 40, 40, 40]
    // [5, 5, 5, 5]
    // [15, 15, 30, 40]
];


// const w = [0.25, 0.25, 0.25, 0.25];
const w = [0.25, 0.25, 0.25, 0.25]
// console.log(wuyuan(qq, x, w), '结果')

module.exports = {wuyuan, jingdainyu}
